// +build !windows

package util

import (
	"syscall"
)

//EnsureOpenFiles ensure the limits of open files
func EnsureOpenFiles(n uint64) error {
	limit := new(syscall.Rlimit)
	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, limit)
	if err != nil {
		return err
	}

	if limit.Cur < n {
		limit.Cur = n
		if limit.Max < n {
			limit.Max = n
		}

		err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, limit)
		if err != nil {
			return err
		}
	}

	return nil
}
